<template>
  <div>
    <!-- 头部组件 -->
    <Nav>
      <h2>图书商城</h2>
      <div @click="wou">我的书架</div>
    </Nav>
    <!-- 搜索框 -->
    <input
      :style="{ borderColor: this.$store.state.color }"
      v-model="input"
      type="text"
      @keypress="sou"
      placeholder="输入书名搜索"
    />
    <!-- 图书内容 -->
    <div class="box">
      <div class="q1" v-for="item in list" :key="item.id">
        <img :src="item.img" class="img" />
        <div class="q2">{{ item.desc }}</div>
        <p>{{ item.name }}</p>
        <div class="iconfont icon-mainongji" @click="add(item)"></div>
      </div>
    </div>
  </div>
</template>
<script>
import Nav from "../components/nav";
export default {
  components: {
    Nav,
  },
  data() {
    return {
      list: [],
      lists: [],
      input: "",
    };
  },
  mounted() {
    this.$axios.get("/data.json").then((res) => {
      this.list = res.data;
      this.lists = res.data;
    });
  },
  methods: {
    // 跳转我的书架
    wou() {
      this.$router.push({ path: "/wou" });
    },
    // 加入书架
    add(item) {
      this.$store.commit("ADD", item);
    },
    // 搜索
    sou() {
      this.list = this.lists.filter((item) => {
        return item.name.includes(this.input);
      });
    },
  },
  computed: {},
  watch: {},
};
</script>
<style scoped>
h2 {
  color: #fff;
}
input {
  height: 40px;
  width: 250px;
  margin-top: 30px;
  margin-left: 60px;
  padding-left: 15px;
}
.box {
  width: 100%;
  height: 500px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.q1 {
  width: 170px;
  height: 280px;
  margin-top: 26px;
}
.q2 {
  width: 100%;
  height: 45px;
  overflow: hidden; /*超出文本隐藏*/
  text-overflow: ellipsis; /*显示省略号*/
  display: -webkit-box;
  -webkit-line-clamp: 2; /*这个数字就代表第多少行 文本隐藏*/
  -webkit-box-orient: vertical;
}
.img {
  width: 160px;
  height: 180px;
  margin-left: 5px;
}
.iconfont {
  float: right;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 30px;
  color: #fff;
  background: red;
  margin-right: 10px;
  text-align: center;
  line-height: 30px;
  margin-top: 10px;
}
p {
  float: left;
  margin-left: 10px;
  color: red;
  margin-top: 10px;
}
</style>